package office.action.sale;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import office.action.sale.form.CustomerFamilyForm;
import office.action.sale.form.CustomerFamilyInfoForm;
import office.action.sale.form.CustomerForm;
import office.bo.sale.impl.CustomerFamily;
import office.bo.sale.impl.CustomerProp;
import office.service.sale.ICustomerFamilyService;
import office.service.sale.ICustomerPropService;

import org.apache.commons.beanutils.BeanUtils;

import platform.common.action.BaseAction;

public class CustomerFamilyAction extends BaseAction {
	
	private static final long serialVersionUID = 1L;
	private ICustomerFamilyService customerFamilyService;
	private List<CustomerFamilyForm> formList = new ArrayList<CustomerFamilyForm>();
	private CustomerFamilyForm form;
	private String id;
	private String customerId;
	@Resource
	private ICustomerPropService customerPropService;
	private List<CustomerProp> cpList;
	private List<CustomerFamilyInfoForm> cfiList;
	private Map<String ,String> cfiMap = new HashMap<String,String>();
	private Map<String ,String> cfiidMap = new HashMap<String,String>();

	/**
	 * 获取所有的客户以及根据条件进行查询
	 * 
	 * @return
	 * @throws Exception
	 */
	public String getCustomerFamilyBuCustomerId() throws Exception {
		List<CustomerFamily> familyList = customerFamilyService.getCustomerFamilyBuCustomerId(customerId);
		for (Iterator<CustomerFamily> iterator = familyList.iterator(); iterator.hasNext();) {
			CustomerFamily customerFamily = (CustomerFamily) iterator.next();
			CustomerFamilyForm customerFamilyForm = new CustomerFamilyForm();
			BeanUtils.copyProperties(customerFamilyForm, customerFamily);
			formList.add(customerFamilyForm);
		}
		return "getCustomerFamilyBuCustomerId";
	}

	/**
	 * 初始化编辑客户页面
	 * @return
	 * @throws Exception
	 */
	public String gotoEditCustomerFamily() throws Exception {
		if(null != id && !"".equals(id)){
			form = customerFamilyService.getById(id);
			for (Iterator<CustomerFamilyInfoForm> iterator = form.getSetInfo().iterator(); iterator.hasNext();) {
				CustomerFamilyInfoForm customerFamilyInfo = (CustomerFamilyInfoForm) iterator.next();
				cfiMap.put(customerFamilyInfo.getCustomerPropId().toString(), customerFamilyInfo.getContent());
				cfiidMap.put(customerFamilyInfo.getCustomerPropId().toString(), customerFamilyInfo.getId().toString());
			}
		}
		cpList = customerPropService.getAllCustomerProp();
		return "gotoEditCustomerFamily";
	}
	
	public String showCustomerFamily() throws Exception {
		if(null!=id&&!"".equals(id)){
			form = customerFamilyService.getById(id);
		}
		return "showCustomerFamily";
	}
	/**
	 * 编辑客户
	 * @return
	 * @throws Exception
	 */
	public String editCustomerFamily() throws Exception {
		try {
			String msg="编辑成功";
			if (null == form.getId()|| ("").equals(form.getId())) {
				msg="新建成功";
				form.setWriteMan(getUserMap().get("userId").toString());
				form.setCreateDate(new Date());
			}
			CustomerForm customerForm = new CustomerForm();
			customerForm.setId(customerId);
			form.setCustomerForm(customerForm);
			form.setUpdateDate(new Date());
			if(cfiList != null)
			form.setSetInfo(new HashSet<CustomerFamilyInfoForm>(cfiList));
			customerFamilyService.editCustomer(form);
			this.getAjaxMessagesJson().setMessage(true, "", msg);
		} catch (Exception e) {
			this.handleException(e);
		}
		return RESULT_AJAXJSON;
	}

	/**
	 * 初始化产品结构列表操作菜单
	 * 
	 * @return
	 * @throws Exception
	 */

	public String gotoCustomerFamilyMenu() throws Exception {
		return "gotoCustomerFamilyMenu";
	}

	/**
	 * 跳转到客户删除确认页面
	 */
	public String gotoDeleteCustomerFamily(){
		return "gotoDeleteCustomerFamily";
	}
	
	/**
	 * 删除客户
	 * @return
	 * @throws Exception
	 */
	public String delCustomerFamily() throws Exception {
		try {
			customerFamilyService.delCustomerFamilyById(id);
			this.getAjaxMessagesJson().setMessage(true, "", "操作成功");
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			throw e;
		}
		return RESULT_AJAXJSON;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public ICustomerPropService getCustomerPropService() {
		return customerPropService;
	}

	public void setCustomerPropService(ICustomerPropService customerPropService) {
		this.customerPropService = customerPropService;
	}

	public List<CustomerProp> getCpList() {
		return cpList;
	}

	public void setCpList(List<CustomerProp> cpList) {
		this.cpList = cpList;
	}

	public List<CustomerFamilyInfoForm> getCfiList() {
		return cfiList;
	}

	public void setCiList(List<CustomerFamilyInfoForm> cfiList) {
		this.cfiList = cfiList;
	}

	public Map<String, String> getCfiMap() {
		return cfiMap;
	}

	public void setCfiMap(Map<String, String> cfiMap) {
		this.cfiMap = cfiMap;
	}

	public ICustomerFamilyService getCustomerFamilyService() {
		return customerFamilyService;
	}

	public void setCustomerFamilyService(
			ICustomerFamilyService customerFamilyService) {
		this.customerFamilyService = customerFamilyService;
	}

	public CustomerFamilyForm getForm() {
		return form;
	}

	public void setForm(CustomerFamilyForm form) {
		this.form = form;
	}

	public List<CustomerFamilyForm> getFormList() {
		return formList;
	}

	public void setFormList(List<CustomerFamilyForm> formList) {
		this.formList = formList;
	}

	public String getCustomerId() {
		return customerId;
	}

	public void setCustomerId(String customerId) {
		this.customerId = customerId;
	}

	public void setCfiList(List<CustomerFamilyInfoForm> cfiList) {
		this.cfiList = cfiList;
	}
}
